package cn.tedu.store.controller;

import cn.tedu.store.service.ex.*;
import cn.tedu.store.util.JsonResult;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * Controller类的基类
 */
public abstract class BaseController {
    protected static final int OK = 2000;
    //@ExceptionHandler，该注解表明指定的方法统一处理异常
    @ExceptionHandler
    public JsonResult<Void> handleServiceException(ServiceException e) {
        JsonResult<Void> jr = new JsonResult<>(e);
        if (e instanceof UsernameDuplicateException) {
            jr.setState(4001);
        } else if (e instanceof UserNotFoundException) {
            jr.setState(4002);
        } else if (e instanceof PasswordNotMatchException) {
            jr.setState(4003);
        } else if (e instanceof ProductNotFoundException) {
            jr.setState(4004);
        } else if (e instanceof InsertException) {
            jr.setState(5001);
        }
        return jr;
    }
}
